package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ResWinningListData;
import com.wefans.wefansapp.common.tool.data.WinningData;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.preference.MFSharedPreference;
import com.wefans.wefansapp.common.tool.utils.MFToastUtil;

public class MywinningActivity extends BaseActivity {

	private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
	
	private Context mContext = null;
	private ArrayList<WinningData> mAlItems = new ArrayList<WinningData>();
	private ListView mLvWinning = null;
	private MywinningAdapter mAdtWinning = null;
	
	private MFSharedPreference mPrf = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mContext = this;
		mPrf = new MFSharedPreference(this);
		
		setContentView(R.layout.activity_main);
		
		initialize();
        installEvent();
	}
	
	@Override
    protected void onPause()
    {
        MFDebug.d(">> onPause()");
        
        notifyDoClaseDrawerMenu();
        
        super.onPause();
    }

    @Override
    protected void notifyDoClaseDrawerMenu()
    {
        MFDebug.d(">> notifyDoClaseDrawerMenu()");
        
        super.notifyDoClaseDrawerMenu();
        
        if( m_DrawerLayout == null ) {
            MFDebug.e(new Exception("There are NO Drawer-Layout"));
            return;
        }
        
        if( m_DrawerView == null ) {
            MFDebug.e(new Exception("There are NO Drawer-View"));
            return;
        }
        
        m_DrawerLayout.closeDrawer(m_DrawerView);
        m_sdQuickMenu.close();
    }
	
	private void initialize()
    {
        MFDebug.d(">> initialize()");
        
        //Contents layout adding
        FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_MAIN_CONTAINER);
        addContentsLayout(containerContents, R.layout.activity_mywinning);
        addQuickMenuLayout(containerContents);
        
        //Drawer menu adding
        m_DrawerView = (FrameLayout) findViewById(R.id.FL_DRAWER_LAYOUT);
        addDrawerLayout(m_DrawerView);
        
        //set drawer layout (root)
        m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_MAIN_NAVIGATION_LAYOUT);
    }
    
    private void installEvent()	
    {
        MFDebug.d(">> installEvent()");
        
        ((ImageButton) findViewById(R.id.IB_MAIN_HEADER_LEFT_BUTTON)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_HEADER_RIGHT_BUTTON)).setOnClickListener(this);                
        
        mLvWinning = (ListView)findViewById(R.id.LV_WINNING);
        mAdtWinning = new MywinningAdapter(mContext, mAlItems);
        mLvWinning.setAdapter(mAdtWinning);
        
        requestWinning();
    }
    
    private void requestWinning() {
    	String strUrl = MFNetworkManager.BASE_URL + "/my/get_win.php";
		strUrl = strUrl + "?mem_idx=" + MFDataManager.getInstance(getApplicationContext()).getMebmerIdx();
		MFNetworkManager.getInstance(this).requestGetData(MFNetworkManager.REQUEST_WINNING_LIST, strUrl, mNetworkListener);
    }
    
    IOnNetworkListener mNetworkListener = new IOnNetworkListener() {
		
		@Override
		public void onNetMessage(int resultCode, NetMessage message) {
			try {
	            //서버 여동 데이터
	            if( message instanceof NetJsonData ) {
	                long magicNum = ((NetJsonData) message).getMagicNum();
	                String json = ((NetJsonData) message).getResponseJson();
	                
	                if( json == null ) {
	                    throw new Exception("invalid json data");
	                }
	                
	                if (magicNum == MFNetworkManager.REQUEST_WINNING_LIST) {
	                	Gson gson = new Gson();
	                	ResWinningListData resultData = (ResWinningListData)gson.fromJson(json, ResWinningListData.class);
	                    if( resultData == null ) {
	                        throw new Exception("Gson parsing error");
	                    }
	                	if (resultData.isSuccsess()) {
	                	} else {
	                		MFToastUtil.NetResponseToast(getApplicationContext(), resultData.getResultcode(), resultData.getResultmsg());
	                		setDummyData();
	                	}
	                }
	            }	            
	        } catch(Exception e) {
	            MFDebug.e(e);
	        }		
		}
	};
    
    private void setDummyData() {
    	WinningData data = new WinningData();
    	data.setId("01");
    	data.setName("2014 드림콘서트 ORANGE ZONE 초대권 2매");
    	data.setDesc("2014.05.01. 14:00 ~ 2014.05.12. 23:00");
    	mAlItems.add(data);
    	
    	data = new WinningData();
    	data.setId("02");
    	data.setName("[컴백 에이핑크 ♡] 신곡 ‘Mr.Chu’로 키스를 부르는 입술을 가진 멤버는 누구~?");
    	data.setDesc("2014.05.01. 14:00 ~ 2014.05.12. 23:00");
    	mAlItems.add(data);
    	
    	data = new WinningData();
    	data.setId("03");
    	data.setName("[Happy Birthday 6월] 회장을 뽑아 주세요!!");
    	data.setDesc("2014.05.01. 14:00 ~ 2014.05.12. 23:00");
    	mAlItems.add(data);
    	
    	mAdtWinning.notifyDataSetChanged();
	}
	
	@Override
	public void onClick(View v) {
		
		Intent intent = null;
		
		switch (v.getId()) {
		case R.id.IB_MAIN_HEADER_LEFT_BUTTON :
            m_DrawerLayout.openDrawer(m_DrawerView);
            break;
		case R.id.IB_MAIN_HEADER_RIGHT_BUTTON:
			intent = new Intent(MywinningActivity.this, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(intent);
			finish();
			break;
		}
		super.onClick(v);
	}
}
